<?php
/*-------------------------------------------------------------------------\
|
|  ========================================================
|  Fusion Registry GPL
|  Copyright (C) 2007 Fusion Scripts
|
|  This program is free software; you can redistribute it and/or
|  modify it under the terms of the GNU General Public License
|  as published by the Free Software Foundation; either version 2
|  of the License, or (at your option) any later version.
|
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
|
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
|  ========================================================
|
|  Email: info@fusionscripts.com
|  Web-Site: http://www.fusionscripts.com/
|
|  $Id: core.php 4 2008-08-03 01:43:03Z registry.fusion $
|
\-------------------------------------------------------------------------*/

/**
 * The Fusion Registry core
 *
 * @package Fusion Registry
 * @subpackage lib
 * @version $Id: core.php 4 2008-08-03 01:43:03Z registry.fusion $
 */
if ( ! defined( 'IN_IPB' ) )
{
    print "<h1>Incorrect access</h1>You cannot access this file directly.";
    exit();
}

/**
 * Fusion Registry custom session handler
 */
require_once REGISTRY_PATH."lib/session.php";

/**
 * Class 'registry_core' contains essential core code
 *
 * @package Fusion Registry
 * @subpackage lib
 */
class registry_core
{
    var $ipsclass;

    /** @var object Parent class object */
    var $class  = "";

    /** @var string */
    var $r_version_short = 'v3.0.4';
    var $r_version_long = '30004';
    var $r_session = "";
    /**#@-*/

    /**
     * Intialization of registry_core
     *
     * Another traffic cop function, there should be minimal procedural code
     * here. We are taking care of the imgs array, and some security here.
     *
     * @param object $class Parent class object
     * @return boolean TRUE
     */
    function auto_run($class)
    {
        $this->class = &$class;

        // Load Config variables
        $this->ipsclass->DB->query("SELECT config_name,config_value FROM ibf_registry_config");

        while ( $row = $this->ipsclass->DB->fetch_row() )
        {
            $this->class->registry_config[ $row['config_name'] ] = $row['config_value'];
        }

        // Just to be safe, let's integer-ize these bad boys
        $cleaners = array( 'id', 'CID', 'MID', 'make_id', 'model_id', 'search' );

        // Make them sparkly clean
        foreach ( $cleaners as $mr_clean )
        {
            if ( isset($this->ipsclass->input[$mr_clean]) )
            {
                $this->ipsclass->input[$mr_clean] = intval($this->ipsclass->input[$mr_clean]);
            }
        }

        // Initialize the registry
        $this->init();

        return TRUE;
    }


    /**
     * Intialize core essential code in the Fusion Registry
     *
     * @return boolean TRUE
     */
    function init()
    {
        // Decode any html entities in our convert options/path
        $this->class->registry_config['convert_options'] = function_exists('html_entity_decode')
                                                         ? html_entity_decode($this->class->registry_config['convert_options'])
                                                         : $this->unhtmlentities($this->class->registry_config['convert_options']);

        $this->class->registry_config['convert_path'] = function_exists('html_entity_decode')
                                                      ? html_entity_decode($this->class->registry_config['convert_path'])
                                                      : $this->unhtmlentities($this->class->registry_config['convert_path']);

        // Prevent illegal division by zero
        $this->class->registry_config['cars_per_page'] = $this->class->registry_config['cars_per_page'] > 0
                                                       ? $this->class->registry_config['cars_per_page']
                                                       : 1;

        // Load our language file
        $this->ipsclass->load_language('lang_registry');

        // We are going to check the browse permission right away.
        // If they don't have browse permission, they don't get
        //   any soup!
        if ( $this->ipsclass->check_perms($this->class->registry_config['browse_perms']) != TRUE )
        {
            $this->ipsclass->Error( array( LEVEL => 1, MSG => 'no_permission') );
        }

        // Define valid skins
        $valid_skins = array( 'core', 'gallery', 'guestbook', 'item', 'main', 'mod' );

        $to_load = $this->class->html;

        // Load the requested skins
        if ( is_array($to_load) )
        {
            foreach ( $to_load as $skin )
            {
                $skin_name = 'skin_registry_'.$skin;

                if ( in_array($skin, $valid_skins) )
                {
                    $this->ipsclass->load_template($skin_name);
                }
            }
        }

        // Define valid libraries
        $valid_libs = array( 'content', 'upload', 'functions', 'custom_fields', 'session', 'ratings' );

        // Grab the list of requested libs to load
        $to_load = $this->class->lib;

        // Force session lib
        if (!in_array('session', $to_load))
        {
            array_push( $to_load, 'session' );
        }

        $this->class->lib = array();

        // Load the requested libraries
        if ( is_array($to_load) )
        {
            foreach ( $to_load as $lib )
            {
                $class_name = 'registry_'.$lib;

                if ( $lib == 'parser' )
                {
                    require_once( ROOT_PATH."sources/handlers/han_parse_bbcode.php" );

                    $this->class->lib['parser'] = new parse_bbcode();
                    $this->class->lib['parser']->ipsclass =& $this->ipsclass;
                }
                elseif ( in_array($lib, $valid_libs) and file_exists(REGISTRY_PATH ."lib/{$lib}.php") )
                {
                    require_once(REGISTRY_PATH ."lib/{$lib}.php");

                    $this->class->lib[$lib] = new $class_name();
                    $this->class->lib[$lib]->ipsclass =& $this->ipsclass;
                    $this->class->lib[$lib]->auto_run($this->class);
                }
            }
        }

        return TRUE;
    }


    /**
     * Appends the program name/version to the output
     *
     * Since skins are now dynamically loaded this depends on the parent class
     * object having already loaded html['core'].
     *
     * @return string Complete HTML output of the Fusion Registry, template 'render_garage'
     */
    function process_output()
    {
        $html = $this->ipsclass->compiled_templates['skin_registry_core']->render_registry($this->class->data);

        $html .= "
<br />
<div class='tablebasic'>
<table cellpadding='4' cellspacing='1' border='0' width='100%'>
    <tr>
        <td class='nopad'><div align='center' class='copyright'><a href='http://www.fusionscripts.com' style='text-decoration:none'>Fusion Registry ". $this->r_version_short ." &copy; 2007 Fusion Scripts</a></div></td>
    </tr>
</table>
</div>";

        return $html;
    }


    /**
     * For users prior to PHP 4.3.0 that do not have the function html_entity_decode
     */
    function unhtmlentities($string)
    {
        // replace numeric entities
        $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
        $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
        // replace literal entities
        $trans_tbl = get_html_translation_table(HTML_ENTITIES);
        $trans_tbl = array_flip($trans_tbl);
        return strtr($string, $trans_tbl);
    }
}

?>